﻿// <copyright file="ResourceStreamResolver.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-01-29</date>
// <summary>XML Resolver used in XSL Transformation to resolve embedded resources.</summary>

namespace Synergist.WordToXamlLibrary
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// Resource stream resolver
    /// </summary>
    public class ResourceStreamResolver : XmlResolver
    {
        /// <summary>
        /// Sets the credentials
        /// </summary>
        public override System.Net.ICredentials Credentials
        {
            set { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets an entity
        /// </summary>
        /// <param name="absoluteUri">the absolute URI</param>
        /// <param name="role">the role of the user</param>
        /// <param name="typeOfObjectToReturn">the object type to return</param>
        /// <returns>a resource stream</returns>
        public override object GetEntity(Uri absoluteUri, string role, Type typeOfObjectToReturn)
        {
            if (absoluteUri == null)
            {
                throw new ArgumentNullException("absoluteUri");
            }

            var thisType = GetType();

            var ns = thisType.Namespace;

            var fileName = Path.GetFileName(absoluteUri.ToString());

            string streamName = ns + "." + fileName;

            var stream = thisType.Assembly.GetManifestResourceStream(streamName);

            return stream;
        }
    }
}
